package com.zzk.lanouhn.myapplication1.adapters;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.zzk.lanouhn.myapplication1.R;
import com.zzk.lanouhn.myapplication1.activity.AuthorsActivity;
import com.zzk.lanouhn.myapplication1.modles.HotMyGridView;

import java.util.List;

/**
 * Created by lanouhn on 16/9/8.
 */
public class ShowHotDetailsAdapter extends RecyclerView.Adapter<ShowHotDetailsAdapter.HotViewHolder>{
    private Context context;
    private List<HotMyGridView.DataBean> hotMyGridView;

    public ShowHotDetailsAdapter(Context context, List<HotMyGridView.DataBean> hotMyGridView) {
        this.context = context;
        this.hotMyGridView = hotMyGridView;
    }

    @Override
    public HotViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(context).inflate(R.layout.item_show_hot_details,null);
        HotViewHolder holder=new HotViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(HotViewHolder holder,  int position) {
        HotMyGridView.DataBean bean=hotMyGridView.get(position);
        Picasso.with(context).load(bean.getAuthor().getAvatar()).into(holder.iv_icon_author);
        Picasso.with(context).load(bean.getMiddle_pic_url()).into(holder.iv_goodsPic);
        holder.tv_name_author.setText(bean.getAuthor().getNickname());
        holder.tv_time.setText(bean.getDatestr());
        holder.tv_contents.setText(bean.getContent());
        holder.tv_contents.setLineSpacing(1,1.5f);

        if (bean.getComments()!= null) {
            holder.tv_count.setText("所有" + bean.getComments().size() + "条评论");
            int number = bean.getComments().size();
            int size = 0;
            if (number >= 3) {
                size = 3;
            } else if (number < 3) {
                size = number;
            }


            for (int i = 0; i < size; i++) {
                View view = LayoutInflater.from(context).inflate(R.layout.item_comments_show, null);
                TextView tv_userName = (TextView) view.findViewById(R.id.tv_name_comments_item);
                TextView tv_comments_content = (TextView) view.findViewById(R.id.tv_comments_content_show);
                tv_comments_content.setText("");
                String authorname = bean.getComments().get(i).getUsername();
                String username = bean.getComments().get(i).getAt_user().getNickname();
                tv_userName.setText(authorname + ":");
                if (username != null) {
                    String content = "回复 @" + username + "" + bean.getComments().get(i).getConent();
                    SpannableStringBuilder spannableStringBuilder = new SpannableStringBuilder(content);
                    spannableStringBuilder.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.mycolor)), 2, username.length() + 4, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    tv_comments_content.setText(spannableStringBuilder);
                } else {
                    tv_comments_content.setText(bean.getComments().get(i).getConent());
                }
                holder.ll_comments.addView(view);
            }
            final int pos=position;
            holder.iv_icon_author.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, AuthorsActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("authorId",hotMyGridView.get(pos).getAuthor().getUser_id());
                    intent.putExtras(bundle);
                    context.startActivity(intent);

                }
            });
            holder.tv_name_author.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent=new Intent(context, AuthorsActivity.class);
                    Bundle bundle=new Bundle();
                    bundle.putString("authorId",hotMyGridView.get(pos).getAuthor().getUser_id());
                    intent.putExtras(bundle);
                    context.startActivity(intent);

                }
            });
        }

    }

    @Override
    public int getItemCount() {
        return hotMyGridView.size();
    }

    public class HotViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout ll_show, ll_comments;
        private TextView tv_name_author, tv_contents, tv_time, tv_title_goods, tv_price_goods, tv_likes, tv_count;
        private ImageView iv_icon_author, iv_goodsPic, iv_comment, iv_like, iv_share, iv_more;
        private ImageView iv_share_pic;


        public HotViewHolder(View itemView) {
            super(itemView);
            ll_show = (LinearLayout) itemView.findViewById(R.id.ll_showhot);
            ll_comments = (LinearLayout) itemView.findViewById(R.id.ll_comments_details_showhot);

            tv_name_author = (TextView) itemView.findViewById(R.id.tv_name_show_hot_details);
            tv_contents = (TextView) itemView.findViewById(R.id.tv_contents_show_hot_details);
            tv_likes = (TextView) itemView.findViewById(R.id.tv_likes_show_hot_details);
            tv_count = (TextView) itemView.findViewById(R.id.tv_count_comments_showhot);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time_show_hot);

            iv_icon_author = (ImageView) itemView.findViewById(R.id.iv_icon_show_hot_details);
            iv_goodsPic = (ImageView) itemView.findViewById(R.id.iv_goodPic_show_hot_details);

        }
    }
}
